from flask import Flask,request,redirect,url_for
from flask import render_template
import os
import uuid
app =Flask(__name__)
app.secret_key='secret_key'
UPLOAD_FOLDER=os.path.join(os.path.dirname(__file__),'upload')
os.makedirs(UPLOAD_FOLDER,exist_ok=True)


@app.route('/upload',methods=['GET','POST'])
def upload():
    if request.method =='POST':

        file=request.files.get('file')
        if file:
            _,ext=os.path.splitext(file.filename)
            safe_name=secure_filename=(file.filename)
            print('_____________________')
            print(f'_={_},ext={ext}')
            print('_____________________')            
            if not safe_name:
                safe_name=f"{uuid.uuid4().hex}{ext}"
            save_path=os.path.join(UPLOAD_FOLDER,safe_name)
            file.save(f'upload/{safe_name}')
            return f"File uploaded successfully:{safe_name}"
        return 'No file uploaded'
    else :
        return render_template('upload.html')

if __name__=="__main__":
    app.run(debug=True)